package com.sixth.statistics.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sixth.core.entity.Comment;
import com.sixth.core.vo.R;
import com.sixth.statistics.mapper.CommentMapper;
import com.sixth.statistics.service.CommentService;
import com.sixth.statistics.vo.CommentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: six-hospital-system
 * @description:
 * @author: 朱永奇
 * @create: 2024-08-20 11:21
 **/
@Service
public class CommentImpl extends ServiceImpl<CommentMapper,Comment> implements CommentService{
    @Autowired
    private CommentMapper commentMapper;
    /**
     * 评级分页显示
     * @param commentVo
     * @return
     */
    @Override
    public R<IPage<Comment>> selectAll(CommentVo commentVo) {
        IPage<Comment> page = new Page<>(commentVo.getPageNum(),commentVo.getPageSize());
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        if(commentVo.getValueId()!=null){
            wrapper.eq("value_id",commentVo.getValueId());
        }
        if (commentVo.getDateRange() != null && commentVo.getDateRange().size() == 2) {
            wrapper.between("add_time", commentVo.getDateRange().get(0), commentVo.getDateRange().get(1));
        }
        wrapper.eq("deleted",0);
        IPage<Comment> commentIPage = commentMapper.selectPage(page,wrapper);
        return R.ok(commentIPage);
    }
    /**
     * 单行删除评价
     * @param id
     * @return
     */
    @Override
    public R deleteById(Integer id) {
        Comment c = new Comment();
        c.setId(id);
        c.setDeleted(1);
        return R.ok(commentMapper.updateById(c));
    }
    /**
     * 批量删除评价
     * @param id
     * @return
     */
    @Override
    public R deleteList(Integer[] id) {
        List<Comment> comments = Arrays.stream(id).map(item ->{
            Comment comment = new Comment();
            comment.setId(item);
            comment.setDeleted(1);
            return comment;
        }).collect(Collectors.toList());
        commentMapper.updateById(comments);
        return R.ok();
    }
}